{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a2b6b5f5",
   "metadata": {},
   "source": [
    "# 1.使用api模板"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57dde124",
   "metadata": {},
   "source": [
    "import requests"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "fc6f00d4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2.准备api的接口身份\n",
    "\n",
    "API_Key = \"AXr0X-fVLzYtJg-Yo13sIqOnA08EUcA1\"\n",
    "API_Secret = \"FgzgKXqhaxO6rcQX7m2kCqkdjBdvBQLg\"\n",
    "\n",
    "# 3.准备base URL(face detect)\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload（酬载）\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"image_url\":\"https://uploadfile.bizhizu.cn/up/38/f4/21/38f421bb04707d4d5cbe25965158262f.jpg\",\n",
    "#    \"return_landmark\":1,\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "79a752b7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "6c4e890d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"request_id\":\"1648465493,ecd3b9d6-9a61-4817-a822-6b36c8445627\",\"time_used\":681,\"faces\":[{\"face_token\":\"ff3787f6c2fde8ca57b733a5159ed26d\",\"face_rectangle\":{\"top\":552,\"left\":422,\"width\":521,\"height\":521},\"attributes\":{\"gender\":{\"value\":\"Female\"},\"age\":{\"value\":26},\"smile\":{\"value\":2.100,\"threshold\":50.000}}}],\"image_id\":\"5csc1oBZA+L7u/YWXD7ahw==\",\"face_num\":1}\\n'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content#用json的形式返回的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d2189077",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648465493,ecd3b9d6-9a61-4817-a822-6b36c8445627',\n",
       " 'time_used': 681,\n",
       " 'faces': [{'face_token': 'ff3787f6c2fde8ca57b733a5159ed26d',\n",
       "   'face_rectangle': {'top': 552, 'left': 422, 'width': 521, 'height': 521},\n",
       "   'attributes': {'gender': {'value': 'Female'},\n",
       "    'age': {'value': 26},\n",
       "    'smile': {'value': 2.1, 'threshold': 50.0}}}],\n",
       " 'image_id': '5csc1oBZA+L7u/YWXD7ahw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "737a1ee0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ff3787f6c2fde8ca57b733a5159ed26d'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()['faces'][0]['face_token']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "68ec1037",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ff3787f6c2fde8ca57b733a5159ed26d'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token = r.json()['faces'][0]['face_token']\n",
    "face_token"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56b6a43e",
   "metadata": {},
   "source": [
    "#### 只要发一次请求，face++就会更新一次，资源会浪费，face72小时会清一次。要想存放资源要将facetoken放在faceset里"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ee123ff2",
   "metadata": {},
   "source": [
    "# 2.人脸分析api"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "05fa3af6",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2.准备api的接口身份\n",
    "\n",
    "API_Key = \"AXr0X-fVLzYtJg-Yo13sIqOnA08EUcA1\"\n",
    "API_Secret = \"FgzgKXqhaxO6rcQX7m2kCqkdjBdvBQLg\"\n",
    "\n",
    "# 3.准备base URL(face detect)\n",
    "analyze_url = \"https://api-cn.faceplusplus.com/facepp/v3/face/analyze\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload（酬载）\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_tokens\":face_token,\n",
    "    \"return_attributes\":\"gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,beauty,mouthstatus,eyegaze,skinstatus\"\n",
    "\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(analyze_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "d439af8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "7f2cc31b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'time_used': 204,\n",
       " 'request_id': '1648465497,fa7a0003-9c67-4415-b969-3bda854e8bf9',\n",
       " 'faces': [{'attributes': {'emotion': {'sadness': 3.576,\n",
       "     'neutral': 93.017,\n",
       "     'disgust': 0.012,\n",
       "     'anger': 0.298,\n",
       "     'surprise': 0.012,\n",
       "     'fear': 0.012,\n",
       "     'happiness': 3.074},\n",
       "    'beauty': {'female_score': 86.69, 'male_score': 86.387},\n",
       "    'gender': {'value': 'Female'},\n",
       "    'age': {'value': 26},\n",
       "    'mouthstatus': {'close': 99.72,\n",
       "     'surgical_mask_or_respirator': 0.0,\n",
       "     'open': 0.28,\n",
       "     'other_occlusion': 0.0},\n",
       "    'glass': {'value': 'None'},\n",
       "    'skinstatus': {'dark_circle': 5.482,\n",
       "     'stain': 7.984,\n",
       "     'acne': 7.828,\n",
       "     'health': 76.981},\n",
       "    'headpose': {'yaw_angle': -11.314982,\n",
       "     'pitch_angle': 12.577334,\n",
       "     'roll_angle': -1.1715746},\n",
       "    'blur': {'blurness': {'threshold': 50.0, 'value': 0.348},\n",
       "     'motionblur': {'threshold': 50.0, 'value': 0.348},\n",
       "     'gaussianblur': {'threshold': 50.0, 'value': 0.348}},\n",
       "    'smile': {'threshold': 50.0, 'value': 2.1},\n",
       "    'eyestatus': {'left_eye_status': {'normal_glass_eye_open': 0.023,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 99.977,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0},\n",
       "     'right_eye_status': {'normal_glass_eye_open': 0.0,\n",
       "      'no_glass_eye_close': 0.0,\n",
       "      'occlusion': 0.0,\n",
       "      'no_glass_eye_open': 100.0,\n",
       "      'normal_glass_eye_close': 0.0,\n",
       "      'dark_glasses': 0.0}},\n",
       "    'facequality': {'threshold': 70.1, 'value': 66.179},\n",
       "    'eyegaze': {'right_eye_gaze': {'position_x_coordinate': 0.379,\n",
       "      'vector_z_component': 0.918,\n",
       "      'vector_x_component': -0.38,\n",
       "      'vector_y_component': -0.115,\n",
       "      'position_y_coordinate': 0.405},\n",
       "     'left_eye_gaze': {'position_x_coordinate': 0.471,\n",
       "      'vector_z_component': 0.987,\n",
       "      'vector_x_component': -0.072,\n",
       "      'vector_y_component': -0.147,\n",
       "      'position_y_coordinate': 0.377}}},\n",
       "   'face_rectangle': {'width': 521, 'top': 552, 'left': 422, 'height': 521},\n",
       "   'face_token': 'ff3787f6c2fde8ca57b733a5159ed26d'}]}"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "699e2775",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "813fa715",
   "metadata": {},
   "source": [
    "## 数据处理 pandas.json_noemalize()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "4653edfb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>face_token</th>\n",
       "      <td>ff3787f6c2fde8ca57b733a5159ed26d</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.sadness</th>\n",
       "      <td>3.576</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.neutral</th>\n",
       "      <td>93.017</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.disgust</th>\n",
       "      <td>0.012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.anger</th>\n",
       "      <td>0.298</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.surprise</th>\n",
       "      <td>0.012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.fear</th>\n",
       "      <td>0.012</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.emotion.happiness</th>\n",
       "      <td>3.074</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.female_score</th>\n",
       "      <td>86.69</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.beauty.male_score</th>\n",
       "      <td>86.387</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.gender.value</th>\n",
       "      <td>Female</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.age.value</th>\n",
       "      <td>26</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.close</th>\n",
       "      <td>99.72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.surgical_mask_or_respirator</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.open</th>\n",
       "      <td>0.28</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.mouthstatus.other_occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.glass.value</th>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.dark_circle</th>\n",
       "      <td>5.482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.stain</th>\n",
       "      <td>7.984</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.acne</th>\n",
       "      <td>7.828</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.skinstatus.health</th>\n",
       "      <td>76.981</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.yaw_angle</th>\n",
       "      <td>-11.314982</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.pitch_angle</th>\n",
       "      <td>12.577334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.headpose.roll_angle</th>\n",
       "      <td>-1.171575</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.blurness.value</th>\n",
       "      <td>0.348</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.motionblur.value</th>\n",
       "      <td>0.348</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.blur.gaussianblur.value</th>\n",
       "      <td>0.348</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.threshold</th>\n",
       "      <td>50.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.smile.value</th>\n",
       "      <td>2.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.023</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.no_glass_eye_open</th>\n",
       "      <td>99.977</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.left_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_open</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.occlusion</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.no_glass_eye_open</th>\n",
       "      <td>100.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.normal_glass_eye_close</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyestatus.right_eye_status.dark_glasses</th>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.threshold</th>\n",
       "      <td>70.1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.facequality.value</th>\n",
       "      <td>66.179</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.379</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_z_component</th>\n",
       "      <td>0.918</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.38</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.115</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.right_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.405</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_x_coordinate</th>\n",
       "      <td>0.471</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_z_component</th>\n",
       "      <td>0.987</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_x_component</th>\n",
       "      <td>-0.072</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.vector_y_component</th>\n",
       "      <td>-0.147</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>attributes.eyegaze.left_eye_gaze.position_y_coordinate</th>\n",
       "      <td>0.377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.width</th>\n",
       "      <td>521</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.top</th>\n",
       "      <td>552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.left</th>\n",
       "      <td>422</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>face_rectangle.height</th>\n",
       "      <td>521</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                                   0\n",
       "face_token                                          ff3787f6c2fde8ca57b733a5159ed26d\n",
       "attributes.emotion.sadness                                                     3.576\n",
       "attributes.emotion.neutral                                                    93.017\n",
       "attributes.emotion.disgust                                                     0.012\n",
       "attributes.emotion.anger                                                       0.298\n",
       "attributes.emotion.surprise                                                    0.012\n",
       "attributes.emotion.fear                                                        0.012\n",
       "attributes.emotion.happiness                                                   3.074\n",
       "attributes.beauty.female_score                                                 86.69\n",
       "attributes.beauty.male_score                                                  86.387\n",
       "attributes.gender.value                                                       Female\n",
       "attributes.age.value                                                              26\n",
       "attributes.mouthstatus.close                                                   99.72\n",
       "attributes.mouthstatus.surgical_mask_or_respirator                               0.0\n",
       "attributes.mouthstatus.open                                                     0.28\n",
       "attributes.mouthstatus.other_occlusion                                           0.0\n",
       "attributes.glass.value                                                          None\n",
       "attributes.skinstatus.dark_circle                                              5.482\n",
       "attributes.skinstatus.stain                                                    7.984\n",
       "attributes.skinstatus.acne                                                     7.828\n",
       "attributes.skinstatus.health                                                  76.981\n",
       "attributes.headpose.yaw_angle                                             -11.314982\n",
       "attributes.headpose.pitch_angle                                            12.577334\n",
       "attributes.headpose.roll_angle                                             -1.171575\n",
       "attributes.blur.blurness.threshold                                              50.0\n",
       "attributes.blur.blurness.value                                                 0.348\n",
       "attributes.blur.motionblur.threshold                                            50.0\n",
       "attributes.blur.motionblur.value                                               0.348\n",
       "attributes.blur.gaussianblur.threshold                                          50.0\n",
       "attributes.blur.gaussianblur.value                                             0.348\n",
       "attributes.smile.threshold                                                      50.0\n",
       "attributes.smile.value                                                           2.1\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                             0.023\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                               0.0\n",
       "attributes.eyestatus.left_eye_status.occlusion                                   0.0\n",
       "attributes.eyestatus.left_eye_status.no_glass_e...                            99.977\n",
       "attributes.eyestatus.left_eye_status.normal_gla...                               0.0\n",
       "attributes.eyestatus.left_eye_status.dark_glasses                                0.0\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                               0.0\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                               0.0\n",
       "attributes.eyestatus.right_eye_status.occlusion                                  0.0\n",
       "attributes.eyestatus.right_eye_status.no_glass_...                             100.0\n",
       "attributes.eyestatus.right_eye_status.normal_gl...                               0.0\n",
       "attributes.eyestatus.right_eye_status.dark_glasses                               0.0\n",
       "attributes.facequality.threshold                                                70.1\n",
       "attributes.facequality.value                                                  66.179\n",
       "attributes.eyegaze.right_eye_gaze.position_x_co...                             0.379\n",
       "attributes.eyegaze.right_eye_gaze.vector_z_comp...                             0.918\n",
       "attributes.eyegaze.right_eye_gaze.vector_x_comp...                             -0.38\n",
       "attributes.eyegaze.right_eye_gaze.vector_y_comp...                            -0.115\n",
       "attributes.eyegaze.right_eye_gaze.position_y_co...                             0.405\n",
       "attributes.eyegaze.left_eye_gaze.position_x_coo...                             0.471\n",
       "attributes.eyegaze.left_eye_gaze.vector_z_compo...                             0.987\n",
       "attributes.eyegaze.left_eye_gaze.vector_x_compo...                            -0.072\n",
       "attributes.eyegaze.left_eye_gaze.vector_y_compo...                            -0.147\n",
       "attributes.eyegaze.left_eye_gaze.position_y_coo...                             0.377\n",
       "face_rectangle.width                                                             521\n",
       "face_rectangle.top                                                               552\n",
       "face_rectangle.left                                                              422\n",
       "face_rectangle.height                                                            521"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.json_normalize(r.json()['faces']).T"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "51a16544",
   "metadata": {},
   "source": [
    "# 3.人脸对比"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5e62dc1a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2.准备api的接口身份\n",
    "\n",
    "API_Key = \"AXr0X-fVLzYtJg-Yo13sIqOnA08EUcA1\"\n",
    "API_Secret = \"FgzgKXqhaxO6rcQX7m2kCqkdjBdvBQLg\"\n",
    "\n",
    "# 3.准备base URL(face detect)\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload（酬载）\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"image_url\":\"https://uploadfile.bizhizu.cn/up/38/f4/21/38f421bb04707d4d5cbe25965158262f.jpg\",\n",
    "#    \"return_landmark\":1,\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "c7c8840e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ecfde9b7b887c82e105657bf88df6c73'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token01 = r.json()[\"faces\"][0][\"face_token\"]\n",
    "face_token01"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "fecec6c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2.准备api的接口身份\n",
    "\n",
    "API_Key = \"AXr0X-fVLzYtJg-Yo13sIqOnA08EUcA1\"\n",
    "API_Secret = \"FgzgKXqhaxO6rcQX7m2kCqkdjBdvBQLg\"\n",
    "\n",
    "# 3.准备base URL(face detect)\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/detect\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload（酬载）\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"image_url\":\"https://img.tt98.com/d/file/tt98/2020051915002008/5ec37c839d655.jpg\",\n",
    "#    \"return_landmark\":1,\n",
    "    \"return_attributes\":\"gender,age,smiling\"\n",
    "\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d940e2a1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'5343233725ca663ef851c6b10c413940'"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "face_token02 = r.json()[\"faces\"][0][\"face_token\"]\n",
    "face_token02"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "97247de0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2.准备api的接口身份\n",
    "\n",
    "API_Key = \"AXr0X-fVLzYtJg-Yo13sIqOnA08EUcA1\"\n",
    "API_Secret = \"FgzgKXqhaxO6rcQX7m2kCqkdjBdvBQLg\"\n",
    "\n",
    "# 3.准备base URL(face detect)\n",
    "compare_url = \"https://api-cn.faceplusplus.com/facepp/v3/compare\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload（酬载）\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"face_token1\":face_token01,\n",
    "    \"face_token2\":face_token02\n",
    "    \n",
    "\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(compare_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "0836b2a4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ef44b07f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648465512,171ce9ba-b3c1-4073-863f-11fc58903e17',\n",
       " 'time_used': 308,\n",
       " 'confidence': 78.635,\n",
       " 'thresholds': {'1e-3': 62.327, '1e-4': 69.101, '1e-5': 73.975}}"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bd3921a2",
   "metadata": {},
   "source": [
    "# 4.封装人脸检测函数，实现快速调用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "8698a53c",
   "metadata": {},
   "outputs": [],
   "source": [
    "API_key = 'AXr0X-fVLzYtJg-Yo13sIqOnA08EUcA1'\n",
    "API_sercret = 'FgzgKXqhaxO6rcQX7m2kCqkdjBdvBQLg'\n",
    "img_url_01 = 'https://uploadfile.bizhizu.cn/up/38/f4/21/38f421bb04707d4d5cbe25965158262f.jpg'\n",
    "img_url_02 = 'https://img.tt98.com/d/file/tt98/2020051915002008/5ec37c839d655.jpg'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "78c743fc",
   "metadata": {},
   "outputs": [],
   "source": [
    "def detect_face(API_key,API_serect,img_url):\n",
    "    \"\"\"face++api人脸检测函数\"\"\"\n",
    "    base_url = 'https://api-cn.faceplusplus.com/facepp/v3/detect'\n",
    "    payload = {\n",
    "        'api_key':API_key,\n",
    "        'api_secret':API_sercret,\n",
    "        'image_url':img_url\n",
    "    }\n",
    "    r = requests.post(url = base_url, params = payload)\n",
    "    return r.json()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "87a5a8d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648465513,4066be58-adf9-4e11-a70c-7fed78b374be',\n",
       " 'time_used': 339,\n",
       " 'faces': [{'face_token': 'f2df90b218c6401546719619e50447e7',\n",
       "   'face_rectangle': {'top': 552, 'left': 422, 'width': 521, 'height': 521}}],\n",
       " 'image_id': '5csc1oBZA+L7u/YWXD7ahw==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_01)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "db005ba5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'request_id': '1648465524,7e0490ce-2835-44ab-b3cc-92c2af7523b2',\n",
       " 'time_used': 1086,\n",
       " 'faces': [{'face_token': '2d7b6f68c712e2bda91838659dfb5da5',\n",
       "   'face_rectangle': {'top': 483, 'left': 275, 'width': 459, 'height': 459}}],\n",
       " 'image_id': 'Fdo4H+iUEYC5NGB8TPnCOg==',\n",
       " 'face_num': 1}"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "detect_face(API_key,API_sercret,img_url_02)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7773d43c",
   "metadata": {},
   "source": [
    "## 创造人脸库"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "30f3c521",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.导入需要的requests模块\n",
    "import requests\n",
    "\n",
    "# 2.准备api的接口身份\n",
    "\n",
    "API_Key = \"AXr0X-fVLzYtJg-Yo13sIqOnA08EUcA1\"\n",
    "API_Secret = \"FgzgKXqhaxO6rcQX7m2kCqkdjBdvBQLg\"\n",
    "\n",
    "# 3.准备base URL(face detect)\n",
    "base_url = \"https://api-cn.faceplusplus.com/facepp/v3/faceset/create\"\n",
    "\n",
    "# 4.请求方法：POST\n",
    "\n",
    "# 5.准备请求参数：payload（酬载）\n",
    "payload = {\n",
    "    \"api_key\":API_Key,\n",
    "    \"api_secret\":API_Secret,\n",
    "    \"image_url\":\"https://uploadfile.bizhizu.cn/up/38/f4/21/38f421bb04707d4d5cbe25965158262f.jpg\",\n",
    "\n",
    "\n",
    "}\n",
    "# 6.请求数据\n",
    "\n",
    "r = requests.post(base_url,payload)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "4439fe08",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "56f1a7d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'{\"faceset_token\": \"c53bfe56eca694f968c222d2bb45f44e\", \"time_used\": 160, \"face_count\": 0, \"face_added\": 0, \"request_id\": \"1648466113,f69b5f58-a807-4cf1-ab5d-65bba9186eb3\", \"outer_id\": \"\", \"failure_detail\": []}'"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.content"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "648d4982",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'faceset_token': 'c53bfe56eca694f968c222d2bb45f44e',\n",
       " 'time_used': 160,\n",
       " 'face_count': 0,\n",
       " 'face_added': 0,\n",
       " 'request_id': '1648466113,f69b5f58-a807-4cf1-ab5d-65bba9186eb3',\n",
       " 'outer_id': '',\n",
       " 'failure_detail': []}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "cdae9658",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
